package com.yuer.edu.controller;

//import com.netflix.discovery.DiscoveryClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Api(value = "AutoDeliverControler",tags = "自动投递简历")
@RestController
@RequestMapping("/autodeliver")
public class AutoDeliverControler {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/checkState/{userId}")
    public Integer findResumeOpenState(@PathVariable Long userId) {
        Integer forObject =
                restTemplate.getForObject("http://localhost:8080/resumer/openstate/"
                        + userId, Integer.class);
        System.out.println("======>>>调⽤简历微服务，获取到⽤户" +
                userId + "的默认简历当前状态为：" + forObject);
        return forObject;
    }

//    @Autowired
//    private DiscoveryClient  discoveryClient;

    @Autowired
    private DiscoveryClient discoveryClient;


    //    @GetMapping("/checkState2/{userId}")
//    public Integer findResumeOpenState2(@PathVariable Long userId) {
//
//        // 从Eureka server中获取我们想要的那个服务信息
//        List<ServiceInstance> instances = discoveryClient.getInstances("yuer-jap-resume");
//        ServiceInstance serviceInstance = instances.get(0);
//        String host = serviceInstance.getHost();
//        int port = serviceInstance.getPort();
//        String url = String.format("http://%s"+":%d"+"/resumer/openstate/",host,port);
//        System.out.println("url: " +url);
//        Integer forObject =
//                restTemplate.getForObject(url
//                        + userId, Integer.class);
//        System.out.println("======>>>调⽤简历微服务，获取到⽤户" +userId + "的默认简历当前状态为：" + forObject);
//        return forObject;
//    }
    @GetMapping("/checkState2/{userId}")
    public Integer findResumeOpenState2(@PathVariable Long userId) {

        // 从Eureka server中获取我们想要的那个服务信息
        List<ServiceInstance> instances = discoveryClient.getInstances("yuer-jap-resume");
        ServiceInstance serviceInstance = instances.get(0);
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
//        String url = String.format("http://%s"+":%d"+"/resumer/openstate/",host,port);
        String url = "http://yuer-jap-resume/resumer/openstate/";
        System.out.println("url: " +url);
        Integer forObject =
                restTemplate.getForObject(url
                        + userId, Integer.class);
        System.out.println("======>>>调⽤简历微服务，获取到⽤户" +userId + "的默认简历当前状态为：" + forObject);
        return forObject;
    }

    /**
     * 提供者模拟处理超时，调用方法添加Hytrix控制
     * @param userId
     * @return
     */
    @HystrixCommand(
            // 细节属性配置
            commandProperties  = {
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")
    })
    @GetMapping("/checkStatusTimeout/{userId}")
    public Integer findResumeOpenStateTimeOUt(@PathVariable Long userId) {

        // 从Eureka server中获取我们想要的那个服务信息
        List<ServiceInstance> instances = discoveryClient.getInstances("yuer-jap-resume");
        ServiceInstance serviceInstance = instances.get(0);
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
//        String url = String.format("http://%s"+":%d"+"/resumer/openstate/",host,port);
        String url = "http://yuer-jap-resume/resumer/openstate/";
        System.out.println("url: " +url);
        Integer forObject =
                restTemplate.getForObject(url
                        + userId, Integer.class);
        System.out.println("======>>>调⽤简历微服务，获取到⽤户" +userId + "的默认简历当前状态为：" + forObject);
        return forObject;
    }
    /**
     * 提供者模拟处理超时，调用方法添加Hytrix控制
     * @param userId
     * @return
     */
    @HystrixCommand(
            // 细节属性配置
            commandProperties  = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")
            },
            fallbackMethod = "fallbackResult"

            )
    @GetMapping("/findResumeOpenStateTimeOUtFallback/{userId}")
    public Integer findResumeOpenStateTimeOUtFallback(@PathVariable Long userId) {

        // 从Eureka server中获取我们想要的那个服务信息
        List<ServiceInstance> instances = discoveryClient.getInstances("yuer-jap-resume");
        ServiceInstance serviceInstance = instances.get(0);
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
//        String url = String.format("http://%s"+":%d"+"/resumer/openstate/",host,port);
        String url = "http://yuer-jap-resume/resumer/openstate/";
        System.out.println("url: " +url);
        Integer forObject =
                restTemplate.getForObject(url
                        + userId, Integer.class);
        System.out.println("======>>>调⽤简历微服务，获取到⽤户" +userId + "的默认简历当前状态为：" + forObject);
        return forObject;
    }

    /*
        定义回退⽅法，返回预设默认值
        注意：该⽅法形参和返回值与原始⽅法保持⼀致
    */
    private String fallbackStrResult( Long userId){
        return  "改接口出现异常，退回进行进行降级";
    }

    private Integer fallbackResult( Long userId){
        System.out.println("改接口出现异常，退回进行进行降级");
        return  -1;
    }
}
